{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Mr4YM6YJTkIz" }, "source": [ "# 2: Python Basics - Variables and Functions" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "kxKbJy3zUGqq" }, "source": [ "## 1. Variables and Operations\n", "There are 4 main types of variables\n", "- int (integer)\n", "- float (decimal number)\n", "- string (string of characters)\n", "- bool (boolean)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": {}, "colab_type": "code", "id": "sB5Lw22eThZs" }, "outputs": [], "source": [ "x = 3 # type int \n", "y = 2.5 # type float\n", "first_name = 'Amine' # type string\n", "z = True # type Bool" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 119 }, "colab_type": "code", "id": "gvy__Ni0VLDg", "outputId": "108dc562-5cdf-493f-f804-572bb84cff36" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x + y = 5.5\n", "x - y = 0.5\n", "x / y = 1.2\n", "x // y = 1.0\n", "x * y = 7.5\n", "x ^ y = 15.588457268119896\n" ] } ], "source": [ "# Arithmetic operations\n", "print('x + y =', x + y)\n", "print('x - y =', x - y)\n", "print('x / y =', x / y)\n", "print('x // y =', x // y) # integer division (very useful for Numpy tables)\n", "print('x * y =', x * y)\n", "print('x ^ y =', x ** y) # x power y" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 85 }, "colab_type": "code", "id": "osU_peQiV5QH", "outputId": "b5e7fe5a-24da-4c26-e26d-fdb680e49f1d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "equality: False\n", "inequality: True\n", "less than or equal to: False\n", "greater or equal: True\n" ] } ], "source": [ "# Comparison operations\n", "print('equality:', x == y)\n", "print('inequality:', x != y)\n", "print('less than or equal to:', x <= y)\n", "print('greater or equal:', x >= y)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "colab_type": "code", "id": "1s4g7tZxWpm0", "outputId": "a9487664-4832-4e20-db7f-801835d4f111" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AND : False\n", "OR : True\n", "XOR : True\n" ] } ], "source": [ "# Opérations Logiques\n", "print('AND :', False and True)\n", "print('OR :', False or True)\n", "print('XOR :', False ^ True)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "z5QvguMPYVgQ" }, "source": [ "Note: Comparison and logic operations used together allow to build basic algorithmic structures (if/esle, while, ...)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "pLJYfhleZRqo" }, "source": [ "## 2. Functions\n", "An anonymous function is a function created with **lambda**. This type of function is basic and is useful to be integrated in the middle of control structures or other functions. It is rarely used." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "8nUYmJJhX7s7", "outputId": "f675d3bc-6d69-43c2-92d9-a69b1c24182a", "tags": [ "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "# Exemple of a lambda function f(x) = x^2\n", "f = lambda x : x**2\n", "\n", "print(f(3))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "n15GdMX1ZdjW", "outputId": "f36347e2-6c28-42e5-d416-9b3799b09fde" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "# Exemple2: g(x, y) = x^2 - y^2\n", "g = lambda x, y : x**2 - y**2\n", "\n", "print(g(4, 2))" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "YjgH-JfFajmb" }, "source": [ "The best way to create a function is to use the following structure: **def**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "kgDxdfuIadt1", "outputId": "32a0469a-d856-4206-931c-b013ae387720" }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Exemple: a function has a name, takes inputs (arguments) and transforms them to return a result\n", "\n", "def function_name(argument_1, argument_2):\n", " result = argument_1 + argument_2\n", " return result\n", "\n", "function_name(3, 2)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "rB-yIhFxbUPa", "outputId": "77716e9a-5364-4be9-dec0-c8c750ac0f46", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "981.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Concrete example : function that calculates the potential energy of a body\n", "\n", "def e_potential(mass, height, g=9.81):\n", " energy = mass * height * g \n", " return energy\n", "\n", "# here g has a default value so we don't have to give it a value\n", "e_potential(mass=10, height=10)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "32X3vNS3cbfE" }, "source": [ "## 3. Exercise and Solution\n", "Modify the e_potential function defined above to return a value indicating whether the computed energy is greater or less than a **limit_energy** passed as the 4th argument" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution :" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "l3SvJ0k_cVUA", "outputId": "e1a10282-d73d-434a-a04a-80a6698a67a5", "tags": [ "hide-cell" ] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def e_potential(mass, height, e_limit, g=9.81):\n", " energy = mass * height * g \n", " return energy > e_limit\n", "\n", "e_potential(mass=10, height=10, e_limit=800)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "oS7eMl_GdFZO" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "authorship_tag": "ABX9TyMFLkJWjpS73et8V2y/zKv/", "include_colab_link": true, "name": "Untitled2.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }